package me.zhengjie.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * AI服务配置类
 * 支持多种AI服务提供商，按优先级顺序尝试调用
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "ai.service")
public class AiServiceConfig {

    /**
     * 启用的AI服务提供商列表（按优先级排序）
     * 支持的类型：openai, deepseek, anthropic, server, local
     */
    private List<String> providers = new ArrayList<>();

    /**
     * OpenAI配置
     */
    private OpenAiProvider openai = new OpenAiProvider();

    /**
     * DeepSeek配置
     */
    private DeepSeekProvider deepseek = new DeepSeekProvider();

    /**
     * Anthropic配置
     */
    private AnthropicProvider anthropic = new AnthropicProvider();

    /**
     * 自定义服务器配置
     */
    private ServerProvider server = new ServerProvider();

    /**
     * 本地LMStudio配置
     */
    private LocalProvider local = new LocalProvider();

    /**
     * 全局配置
     */
    private GlobalConfig global = new GlobalConfig();

    @Data
    public static class OpenAiProvider {
        private boolean enabled = false;
        private String apiKey;
        private String baseUrl = "https://api.openai.com";
        private String model = "gpt-3.5-turbo";
        private boolean useImageInput = true;
        private double temperature = 0.3;
        private int maxTokens = 10000;
        private int timeout = 60000; // 60秒
    }

    @Data
    public static class DeepSeekProvider {
        private boolean enabled = false;
        private String apiKey;
        private String baseUrl = "https://api.deepseek.com";
        private String model = "deepseek-chat";
        private boolean useImageInput = true;
        private double temperature = 0.3;
        private int maxTokens = 10000;
        private int timeout = 60000;
    }

    @Data
    public static class AnthropicProvider {
        private boolean enabled = false;
        private String apiKey;
        private String baseUrl = "https://api.anthropic.com";
        private String model = "claude-3-sonnet-20240229";
        private boolean useImageInput = true;
        private double temperature = 0.3;
        private int maxTokens = 10000;
        private int timeout = 60000;
    }

    @Data
    public static class ServerProvider {
        private boolean enabled = false;
        private String apiKey;
        private String baseUrl;
        private String model = "gemini-2.5-pro"; // 默认模型
        private boolean useImageInput = true;
        private double temperature = 0.3;
        private int maxTokens = 10000;
        private int timeout = 60000;
        private String apiType = "openai"; // API类型：openai, gemini, custom
    }

    @Data
    public static class LocalProvider {
        private boolean enabled = true; // 默认启用本地服务作为后备
        private String apiKey = "lm-studio-local";
        private String baseUrl = "http://169.254.226.90:1234";
        private String model = "gpt-3.5-turbo";
        private boolean useImageInput = true;
        private double temperature = 0.3;
        private int maxTokens = 10000;
        private int timeout = 60000;
    }

    @Data
    public static class GlobalConfig {
        private int retryCount = 3;
        private int retryDelayMs = 1000;
        private boolean fallbackToLocal = true; // 如果所有第三方服务失败，是否回退到本地服务
    }

    /**
     * 获取启用的AI服务提供商配置
     */
    public Object getProviderConfig(String providerType) {
        switch (providerType.toLowerCase()) {
            case "openai":
                return openai.isEnabled() ? openai : null;
            case "deepseek":
                return deepseek.isEnabled() ? deepseek : null;
            case "anthropic":
                return anthropic.isEnabled() ? anthropic : null;
            case "server":
                return server.isEnabled() ? server : null;
            case "local":
                return local.isEnabled() ? local : null;
            default:
                return null;
        }
    }

    /**
     * 检查提供商是否启用
     */
    public boolean isProviderEnabled(String providerType) {
        Object config = getProviderConfig(providerType);
        return config != null;
    }
}
